package com.xiaoyu.izf.controller;

import com.xiaoyu.izf.aspect.RequestAuth;
import com.xiaoyu.izf.model.CommissionGetLog;
import com.xiaoyu.izf.model.UserCardInfo;
import com.xiaoyu.izf.model.page.DataTableBean;
import com.xiaoyu.izf.model.page.FormResultBean;
import com.xiaoyu.izf.model.page.ParamListBean;
import com.xiaoyu.izf.model.page.Status;
import com.xiaoyu.izf.service.CommissionGetLogService;
import com.xiaoyu.izf.service.UserCardInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Controller
@RequestMapping("/commission_get_log")
public class CommissionGetLogController extends BaseController {

    @Autowired
    private CommissionGetLogService commissionGetLogService;
    @Autowired
    private UserCardInfoService userCardInfoService;

    @RequestMapping("/list_page")
    @RequestAuth
    public String listPage(){
        return "mgr/commission_get_log/list";
    }
    @RequestMapping("/list")
    @ResponseBody
    @RequestAuth
    public DataTableBean list(@RequestBody String body) throws IOException {
        ParamListBean paramListBean = getParamListBean(body);
        DataTableBean dataTableBean = commissionGetLogService.list(paramListBean);
        return dataTableBean;
    }

    @RequestMapping("/par_list_page")
    @RequestAuth
    public String parListPage(HttpServletRequest request, Model model){
        model.addAttribute("uid",user(request).getId());
        return "mgr/commission_get_log/par_list";
    }
    @RequestMapping("/par_list")
    @ResponseBody
    @RequestAuth
    public DataTableBean parList(@RequestBody String body) throws IOException {
        ParamListBean paramListBean = getParamListBean(body);
        DataTableBean dataTableBean = commissionGetLogService.list(paramListBean);
        return dataTableBean;
    }

    @RequestMapping("/cur_user_list_page")
    @RequestAuth
    public String curUserListPage(int uid, Model model){
        model.addAttribute("uid",uid);
        return "mgr/commission_get_log/cur_user_list";
    }
    @RequestMapping("/cur_user_list")
    @ResponseBody
    @RequestAuth
    public DataTableBean curUserList(@RequestBody String body) throws IOException {
        ParamListBean paramListBean = getParamListBean(body);
        DataTableBean dataTableBean = commissionGetLogService.list(paramListBean);
        return dataTableBean;
    }

    /**
     * 提现打款
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/commission_pay_page")
    @RequestAuth
    public String commissionPayPage(HttpServletRequest request,int id,Model model) {
        CommissionGetLog getLog = commissionGetLogService.findById(id);
        UserCardInfo cardInfo = userCardInfoService.findById(getLog.getUid());
        model.addAttribute("getLog",getLog);
        if (cardInfo!=null){
            model.addAttribute("cardInfo",cardInfo);
        }
        return "/mgr/commission_get_log/commission_pay";
    }
    @RequestMapping("/commission_pay")
    @RequestAuth
    @ResponseBody
    public FormResultBean commissionPay(int id){
        CommissionGetLog getLog = commissionGetLogService.findById(id);
        FormResultBean fr = new FormResultBean(Status.HTTP_OK.value());
        if (getLog==null)
            return fr.setType("error").setMessage("数据不存在");
        boolean flag = commissionGetLogService.commissionPay(getLog);
        if (!flag)
            return fr.setType("error").setMessage("提现失败");

        return new FormResultBean(Status.HTTP_OK.value(),"success","提现成功");
    }
}
